package portlet.ctct;

import java.io.IOException;

import java.sql.Timestamp;

import java.text.ParseException;
import java.text.SimpleDateFormat;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.ResourceBundle;
import java.util.StringTokenizer;
import java.util.TimeZone;

import javax.el.ELContext;
import javax.el.ExpressionFactory;
import javax.el.ValueExpression;

import javax.faces.application.Application;
import javax.faces.context.FacesContext;
import javax.faces.convert.DateTimeConverter;

import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.PortletException;
import javax.portlet.PortletPreferences;
import javax.portlet.PortletRequest;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;

import oracle.adf.model.binding.DCIteratorBinding;
import oracle.adf.view.rich.component.rich.data.RichColumn;
import oracle.adf.view.rich.component.rich.data.RichTable;
import oracle.adf.view.rich.component.rich.layout.RichPanelBorderLayout;
import oracle.adf.view.rich.component.rich.layout.RichPanelFormLayout;
import oracle.adf.view.rich.component.rich.layout.RichPanelLabelAndMessage;
import oracle.adf.view.rich.component.rich.nav.RichGoLink;
import oracle.adf.view.rich.component.rich.output.RichImage;
import oracle.adf.view.rich.component.rich.output.RichOutputText;
import oracle.adf.view.rich.context.AdfFacesContext;

import oracle.jbo.Row;
import oracle.jbo.RowSet;
import oracle.jbo.domain.ClobDomain;
import oracle.jbo.domain.Date;
import oracle.jbo.domain.Number;

import oracle.portlet.bridge.adf.application.ADFBridgePortlet;

import portlet.common.Utils;
import portlet.common.search.AdvancedSearchGroup;
import portlet.common.search.AdvancedSearchItem;


public class ChitietCt extends ADFBridgePortlet {
    private RichPanelFormLayout chiTietPanel;

    protected String getTitle(RenderRequest request) {
        // Get the customized title. This defaults to the declared title.
        PortletPreferences prefs = request.getPreferences();
        return prefs.getValue(PORTLETTITLE_KEY, super.getTitle(request));
    }
    public static final String APPLY_ACTION = "apply_action";
    public static final String OK_ACTION = "ok_action";
    public static final String PORTLETTITLE_KEY = "portletTitle";
    private static final String DEFAULT_LANGCODE = "vi";
    private final String SEARCH_ID_PREFIX = "search";
    private final String UPDATE_ID_PREFIX = "update";
    private final String ROW_KEY = "rowkey";
    private final String FILE_GROUP_ID_SUBFIX = "group";
    private final String FILE_LABEL_ID_SUBFIX = "label";
    private final String FILE_REMOVE_ID_SUBFIX = "remove";

    private String[] buildValueArray(String values) {
        if (values.indexOf(',') < 0) {
            return new String[] { values };
        }
        StringTokenizer st = new StringTokenizer(values, ",");
        String[] valueArray = new String[st.countTokens()];
        int i = 0;
        while (st.hasMoreTokens()) {
            valueArray[i] = st.nextToken().trim();
            i++;
        }
        return valueArray;
    }

    public void processAction(ActionRequest request,
                              ActionResponse response) throws PortletException,
                                                              IOException {
        request.setAttribute("javax.portlet.config", getPortletConfig());
        super.processAction(request, response);
    }

    public void render(RenderRequest request,
                       RenderResponse response) throws PortletException,
                                                       IOException {
        request.setAttribute("javax.portlet.config", getPortletConfig());
        super.render(request, response);
    }

    @Override
    protected void doView(RenderRequest renderRequest,
                          RenderResponse renderResponse) throws PortletException,
                                                                IOException {
        renderRequest.getPortletSession().setAttribute("reset", 1);
        checkParameter(renderRequest);
        super.doView(renderRequest, renderResponse);
    }
    
    public boolean checkParameter(RenderRequest request) {
        boolean check = true;
        String pLoaiDonViId = request.getParameter("loaidonviid");
        String pLangCode = request.getParameter("langcode");
        String pHtFileServletUrl = request.getParameter("htfileservleturl");
        String pDonViId = request.getParameter("donviid");
        String sLoaiDonViId =
            (String)request.getPortletSession().getAttribute("loaidonviid");
        String sLangCode =
            (String)request.getPortletSession().getAttribute("langcode");
        String sDonViId =
            (String)request.getPortletSession().getAttribute("donviid");
        String sHtFileServletUrl =
            (String)request.getPortletSession().getAttribute("htfileservleturl");
        if (pLangCode == null) {
            pLangCode = DEFAULT_LANGCODE;
        }
        if (sLoaiDonViId != null && pLoaiDonViId != null) {
            if (sLoaiDonViId.equals(pLoaiDonViId) == false) {
                check = false;
            }
        } else if (pLoaiDonViId != null) {
            check = false;
        } else if (sLoaiDonViId != null) {
            check = false;
        }

        if (sLangCode != null && pLangCode != null) {
            if (sLangCode.equals(pLangCode) == false) {
                check = false;
            }
        } else if (pLangCode != null) {
            check = false;
        } else if (sLangCode != null) {
            check = false;
        }

        if (sDonViId != null && pDonViId != null) {
            if (sDonViId.equals(pDonViId) == false) {
                check = false;
            }
        } else if (pDonViId != null) {
            check = false;
        } else if (sDonViId != null) {
            check = false;
        }

        if (sHtFileServletUrl!= null && pHtFileServletUrl != null) {
            if (sHtFileServletUrl.equals(pHtFileServletUrl) == false) {
                check = false;
            }
        } else if (pHtFileServletUrl != null) {
            check = false;
        } else if (sHtFileServletUrl != null) {
            check = false;
        }
        request.getPortletSession().setAttribute("loaidonviid", pLoaiDonViId);
        request.getPortletSession().setAttribute("langcode", pLangCode);
        request.getPortletSession().setAttribute("donviid", pDonViId);
        request.getPortletSession().setAttribute("htfileservleturl", pHtFileServletUrl);
        return check;
    }

    public String getResetForm() {
        PortletRequest request =
            (PortletRequest)FacesContext.getCurrentInstance().getExternalContext().getRequest();
        Integer reset =
            (Integer)request.getPortletSession().getAttribute("reset");
        if (reset != null && reset.intValue() == 1) {
            resetIterator(request);
            createForm(request, getValue(request));
            request.getPortletSession().setAttribute("reset", 0);
            AdfFacesContext.getCurrentInstance().addPartialTarget(chiTietPanel);
        }
        return null;
    }

    private void createForm(PortletRequest request,
                            HashMap<String, Object> mapValue) {
        chiTietPanel.getChildren().clear();
        if (mapValue != null) {
            String langCode =
                (String)request.getPortletSession().getAttribute("langcode");
            if (langCode == null) {
                langCode = DEFAULT_LANGCODE;
            }
            DCIteratorBinding truongTtView1Iterator =
                Utils.getIteratorBinding("DlctTruongTtView1Iterator");
            truongTtView1Iterator.setRangeSize(-1);
            Row[] rows = truongTtView1Iterator.getAllRowsInRange();
        if(rows==null){
            rows=new Row[]{};
        }
            for (int i = 0; i < rows.length; i++) {
                String ten = (String)rows[i].getAttribute("TenTruong");
                Number kieuDuLieu = (Number)rows[i].getAttribute("KieuDuLieu");
                Number viTriHienThi =
                    (Number)rows[i].getAttribute("ViTriHienThi");
                Row loopRow = rows[i];
                Row rootRow = null;
                while (loopRow != null) {
                    rootRow = loopRow;
                    loopRow =
                            (Row)loopRow.getAttribute("DlctTruongTtTuIdDlctTruongTtView");
                }
                String rootId =
                    ((Number)rootRow.getAttribute("Id")).toString();
                if (viTriHienThi != null && kieuDuLieu != null) {
                    int viTri = viTriHienThi.intValue();
                    int kieu = kieuDuLieu.intValue();
                    if ((Utils.ENUM_VI_TRI_HIEN_THI.PORTLET_INFO.getId() &
                         viTri) ==
                        Utils.ENUM_VI_TRI_HIEN_THI.PORTLET_INFO.getId()) {
                        if (kieu ==
                            Utils.ENUM_KIEU_DU_LIEU.IN_OUT_KIEM_TOAN_VIEN.getId() ||
                            kieu ==
                            Utils.ENUM_KIEU_DU_LIEU.KIEM_TOAN_VIEN.getId() ||
                            kieu ==
                            Utils.ENUM_KIEU_DU_LIEU.SL_KIEM_TOAN_VIEN.getId()) {
                            ten=ten.replace("{0}", ""+Calendar.getInstance().get(Calendar.YEAR));
                            if (kieu ==
                                Utils.ENUM_KIEU_DU_LIEU.SL_KIEM_TOAN_VIEN.getId()) {
                                List<AdvancedSearchGroup> listASG =
                                    new ArrayList();
                                AdvancedSearchGroup asg =
                                    new AdvancedSearchGroup();
                                asg.setConjunction(asg.VC_CONJ_OR);
                                listASG.add(asg);
                                AdvancedSearchItem asi =
                                    new AdvancedSearchItem();
                                asg.getListChildren().add(asi);
                                asi.setItemName("NgayKetThuc");
                                asi.setOperator(asi.OPER_IS_BLANK);
                                asi.setRequired(true);
                                asg = new AdvancedSearchGroup();
                                asg.setConjunction(asg.VC_CONJ_OR);
                                listASG.add(asg);
                                asi = new AdvancedSearchItem();
                                asg.getListChildren().add(asi);
                                asi.setItemName("NgayKetThuc");
                                asi.setOperator(asi.OPER_AFTER);
                                asi.setRequired(true);
                                asi.getListValue().add(0,
                                                       new Date(new Timestamp(Calendar.getInstance().getTimeInMillis())));
                                Utils.searchadvanced("DlctKtvDvView1Iterator",
                                                     listASG);
                                DCIteratorBinding ktvDvView1Iterator =
                                    Utils.getIteratorBinding("DlctKtvDvView1Iterator");
                                if(ktvDvView1Iterator.getEstimatedRowCount()>0){
                                RichPanelLabelAndMessage label =
                                    new RichPanelLabelAndMessage();
                                label.setLabel(ten);
                                RichOutputText outputText =
                                    new RichOutputText();
                                outputText.setValue(ktvDvView1Iterator.getEstimatedRowCount());
                                label.getChildren().add(outputText);
                                chiTietPanel.getChildren().add(label);
                                }
                            }
                            if (kieu ==
                                Utils.ENUM_KIEU_DU_LIEU.KIEM_TOAN_VIEN.getId()) {
                                RichTable tableKtv = new RichTable();
                                tableKtv.setAutoHeightRows(1000);
                                tableKtv.setStyleClass("ubck-table AFStretchWidth");
                                tableKtv.setRowBandingInterval(1);
                                tableKtv.setColumnResizing("disabled");
                                tableKtv.setContentDelivery("immediate");
                                tableKtv.setInlineStyle("height:auto !important;");
                                tableKtv.setFetchSize(1000);
                                tableKtv.setVar("row");
                                tableKtv.setInlineStyle("margin-top: 0px");
                                FacesContext fc =
                                    FacesContext.getCurrentInstance();
                                Application app = fc.getApplication();
                                ExpressionFactory elFactory =
                                    app.getExpressionFactory();
                                ELContext elContext = fc.getELContext();
                                RichColumn column = new RichColumn();
                            column.setNoWrap(false);
                                column.setSeparateRows(true);
                                column.setRowHeader(false);
                                column.setWidth("20");
                                column.setAlign(RichColumn.ALIGN_CENTER);
                                column.setHeaderText("STT");
                                RichOutputText outputText =
                                    new RichOutputText();
                                ValueExpression valueExp =
                                    elFactory.createValueExpression(elContext,
                                                                    "#{row['STT']}",
                                                                    Object.class);
                                outputText.setValueExpression("value",
                                                              valueExp);
                                column.getChildren().add(outputText);
                                tableKtv.getChildren().add(column);
                                {
                                    List<AdvancedSearchGroup> listASG =
                                        new ArrayList();
                                    AdvancedSearchGroup asgTrangThai =
                                        new AdvancedSearchGroup();
                                    listASG.add(asgTrangThai);
                                    AdvancedSearchItem asiTrangThai =
                                        new AdvancedSearchItem();
                                    asgTrangThai.getListChildren().add(asiTrangThai);
                                    asiTrangThai.setItemName("TrangThai");
                                    asiTrangThai.setRequired(true);
                                    asiTrangThai.getListValue().add(0,
                                                                    new Number(Utils.ENUM_TRANG_THAI.CONG_BO.getId()));
                                    AdvancedSearchGroup asgLoaiTruongTt =
                                        new AdvancedSearchGroup();
                                    listASG.add(asgLoaiTruongTt);
                                    AdvancedSearchItem asiLoaiTruongTt =
                                        new AdvancedSearchItem();
                                    asgLoaiTruongTt.getListChildren().add(asiLoaiTruongTt);
                                    asiLoaiTruongTt.setItemName("DlctLoaiTruongTt");
                                    asiLoaiTruongTt.setRequired(true);
                                    asiLoaiTruongTt.getListValue().add(0,
                                                                       new Number(Utils.ENUM_LOAI_TRUONG_TT.KIEM_TOAN_VIEN.getId()));
                                    if (langCode != null) {
                                        try {
                                            AdvancedSearchGroup asg =
                                                new AdvancedSearchGroup();
                                            listASG.add(asg);
                                            AdvancedSearchItem asi =
                                                new AdvancedSearchItem();
                                            asg.getListChildren().add(asi);
                                            asi.setItemName("DlctNgonNguView");
                                            asi.setRequired(true);
                                            asi.setOperator(AdvancedSearchItem.OPER_EXISTS);
                                            AdvancedSearchGroup asgChild =
                                                new AdvancedSearchGroup();
                                            asi.getListChildren().add(asgChild);
                                            AdvancedSearchItem asiChild =
                                                new AdvancedSearchItem();
                                            asgChild.getListChildren().add(asiChild);
                                            asiChild.setItemName("Ma");
                                            asiChild.setRequired(true);
                                            asiChild.getListValue().add(langCode);
                                        } catch (Exception e) {
                                        }
                                    }
                                    Utils.searchadvanced("DlctTruongTtView2Iterator",
                                                         listASG);
                                    DCIteratorBinding truongTtView2Iterator =
                                        Utils.getIteratorBinding("DlctTruongTtView2Iterator");

                                    truongTtView2Iterator.setRangeSize(-1);
                                    Row[] rowsKtv =
                                        truongTtView2Iterator.getAllRowsInRange();
        if(rowsKtv==null){
            rowsKtv=new Row[]{};
        }
                                    for (int j = 0; j < rowsKtv.length; j++) {
                                        String tenKtv =
                                            (String)rowsKtv[j].getAttribute("TenTruong");
                                        Number kieuDuLieuKtv =
                                            (Number)rowsKtv[j].getAttribute("KieuDuLieu");
                                        Number viTriHienThiKtv =
                                            (Number)rowsKtv[j].getAttribute("ViTriHienThi");
                                        Row loopRowKtv = rowsKtv[j];
                                        Row rootRowKtv = null;
                                        while (loopRowKtv != null) {
                                            rootRowKtv = loopRowKtv;
                                            loopRowKtv =
                                                    (Row)loopRowKtv.getAttribute("DlctTruongTtTuIdDlctTruongTtView");
                                        }
                                        if (viTriHienThiKtv != null &&
                                            kieuDuLieuKtv != null) {
                                            int viTriKtv =
                                                viTriHienThiKtv.intValue();
                                            int kieuKtv =
                                                kieuDuLieuKtv.intValue();
                                            if ((Utils.ENUM_VI_TRI_HIEN_THI.PORTLET_TABLE.getId() &
                                                 viTriKtv) ==
                                                Utils.ENUM_VI_TRI_HIEN_THI.PORTLET_TABLE.getId()) {
                                                String rootIdKtv =
                                                    ((Number)rootRowKtv.getAttribute("Id")).toString();
                                                if (kieuKtv ==
                                                    Utils.ENUM_KIEU_DU_LIEU.IN_OUT_KIEM_TOAN_VIEN.getId() ||
                                                    kieuKtv ==
                                                    Utils.ENUM_KIEU_DU_LIEU.KIEM_TOAN_VIEN.getId() ||
                                                    kieuKtv ==
                                                    Utils.ENUM_KIEU_DU_LIEU.SL_KIEM_TOAN_VIEN.getId()) {
                                                    // khong dua ra giao dien bang
                                                } else {
                                                    column = new RichColumn();
                            column.setNoWrap(false);
                                                    column.setHeaderText(tenKtv);
                                                    outputText =
                                                            new RichOutputText();
                                                    if (kieuKtv ==Utils.ENUM_KIEU_DU_LIEU.DATE.getId()){
            DateTimeConverter converter = (DateTimeConverter)FacesContext.getCurrentInstance().getApplication()
            .createConverter("javax.faces.DateTime");
                                converter.setLocale(new Locale(langCode));
                                converter.setTimeZone(TimeZone.getTimeZone("GMT+7:00"));
                                converter.setType("date");
                                converter.setDateStyle("short");
                                                    outputText.setConverter(converter);
                                                    }
                                                    valueExp =
                                                            elFactory.createValueExpression(elContext,
                                                                                            "#{row['" +
                                                                                            rootIdKtv +
                                                                                            "']}",
                                                                                            Object.class);
                                                    outputText.setValueExpression("value",
                                                                                  valueExp);
                                                    column.getChildren().add(outputText);
                                                    tableKtv.getChildren().add(column);
                                                }
                                            }
                                        }
                                    }
                                }
                                {
                                    Date current =
                                        new Date(new Timestamp(Calendar.getInstance().getTimeInMillis()));
                                    List<AdvancedSearchGroup> listASG =
                                        new ArrayList();
                                    AdvancedSearchGroup asg =
                                        new AdvancedSearchGroup();
                                    asg.setConjunction(asg.VC_CONJ_OR);
                                    listASG.add(asg);
                                    AdvancedSearchItem asi =
                                        new AdvancedSearchItem();
                                    asg.getListChildren().add(asi);
                                    asi.setItemName("NgayKetThuc");
                                    asi.setOperator(asi.OPER_IS_BLANK);
                                    asi.setRequired(true);
                                    asg = new AdvancedSearchGroup();
                                    asg.setConjunction(asg.VC_CONJ_OR);
                                    listASG.add(asg);
                                    asi = new AdvancedSearchItem();
                                    asg.getListChildren().add(asi);
                                    asi.setItemName("NgayKetThuc");
                                    asi.setOperator(asi.OPER_AFTER);
                                    asi.setRequired(true);
                                    asi.getListValue().add(0, current);
                                    Utils.searchadvanced("DlctKtvDvView1Iterator",
                                                         listASG);
                                    DCIteratorBinding ktvDvView1Iterator =
                                        Utils.getIteratorBinding("DlctKtvDvView1Iterator");
                                    ktvDvView1Iterator.setRangeSize(-1);
                                    Row[] rowsKtv =
                                        ktvDvView1Iterator.getAllRowsInRange();
                                    List<Map<String, Object>> listValue =
                                        new ArrayList();
                                    if (rowsKtv != null&&rowsKtv.length>0) {
                                        for (int j = 0; j < rowsKtv.length;
                                             j++) {
                                            ktvDvView1Iterator.setCurrentRowIndexInRange(j);
                                            Row kiemToanVienRow =
                                                (Row)ktvDvView1Iterator.getCurrentRow().getAttribute("DlctKiemToanVienView");
                                            if (kiemToanVienRow != null) {
                                                RowSet rowSetGtTt =
                                                    (RowSet)kiemToanVienRow.getAttribute("DlctGtKtvTtView");
                                                if (rowSetGtTt != null) {
                                    rowSetGtTt.setRangeSize(-1);
                                                    Row[] rowsGtTt =
                                                        rowSetGtTt.getAllRowsInRange();
        if(rowsGtTt==null){
            rowsGtTt=new Row[]{};
        }
                                                    HashMap<String, Object> mapValueKtv =
                                                        new HashMap();
                                                    mapValueKtv.put("STT",j + 1);
                                                    for (int k = 0;
                                                         k < rowsGtTt.length;
                                                         k++) {
                                                        Row truongTtRow =
                                                            (Row)rowsGtTt[k].getAttribute("DlctTruongTtView");
                                                        if (truongTtRow !=
                                                            null) {
                                                            Number idKtvTt =
                                                                (Number)truongTtRow.getAttribute("Id");
                                                            Number kieuDuLieuKtvTt =
                                                                (Number)truongTtRow.getAttribute("KieuDuLieu");
                                                            if (idKtvTt !=
                                                                null &&
                                                                kieuDuLieuKtvTt !=
                                                                null) {
                                                                int kieuKtvTt =
                                                                    kieuDuLieuKtvTt.intValue();
                                                                if (kieuKtvTt ==
                                                                    Utils.ENUM_KIEU_DU_LIEU.NUMBER.getId()) {
                                                                    Number valNumber =
                                                                        (Number)rowsGtTt[k].getAttribute("ValNumber");
                                                                    if (valNumber !=
                                                                        null) {
                                                                        mapValueKtv.put(idKtvTt.toString(),
                                                                                        valNumber.toString());
                                                                    }
                                                                }
                                                                if (kieuKtvTt ==
                                                                    Utils.ENUM_KIEU_DU_LIEU.VARCHAR.getId()) {
                                                                    String valVarchar =
                                                                        (String)rowsGtTt[k].getAttribute("ValVarchar");
                                                                    if (valVarchar !=
                                                                        null) {
                                                                        mapValueKtv.put(idKtvTt.toString(),
                                                                                        valVarchar);
                                                                    }
                                                                }
                                                                if (kieuKtvTt ==
                                                                    Utils.ENUM_KIEU_DU_LIEU.LINK.getId()) {
                                                                    String valVarchar =
                                                                        (String)rowsGtTt[k].getAttribute("ValVarchar");
                                                                    if (valVarchar !=
                                                                        null) {
                                                                        mapValueKtv.put(idKtvTt.toString(),
                                                                                        valVarchar);
                                                                    }
                                                                }
                                                                if (kieuKtvTt ==
                                                                    Utils.ENUM_KIEU_DU_LIEU.EMAIL.getId()) {
                                                                    String valVarchar =
                                                                        (String)rowsGtTt[k].getAttribute("ValVarchar");
                                                                    if (valVarchar !=
                                                                        null) {
                                                                        mapValueKtv.put(idKtvTt.toString(),
                                                                                        valVarchar);
                                                                    }
                                                                }
                                                                if (kieuKtvTt ==
                                                                    Utils.ENUM_KIEU_DU_LIEU.DATE.getId()) {
                                                                    oracle.jbo.domain.Date valDate =
                                                                        (oracle.jbo.domain.Date)rowsGtTt[k].getAttribute("ValDate");
                                                                    if (valDate !=
                                                                        null) {
                                                                        java.util.Date date =
                                                                            valDate.dateValue();
                                                                        if (date !=
                                                                            null) {
                                                                            mapValueKtv.put(idKtvTt.toString(),
                                                                                            date);
                                                                        }
                                                                    }
                                                                }
                                                                if (kieuKtvTt ==
                                                                    Utils.ENUM_KIEU_DU_LIEU.CLOB.getId()) {
                                                                    ClobDomain valClob =
                                                                        (ClobDomain)rowsGtTt[k].getAttribute("ValClob");
                                                                    if (valClob !=
                                                                        null) {
                                                                        mapValueKtv.put(idKtvTt.toString(),
                                                                                        valClob.toString());
                                                                    }
                                                                }
                                                                if (kieuKtvTt ==
                                                                    Utils.ENUM_KIEU_DU_LIEU.FILE.getId()) {
                                                                    Row fileViewRow =
                                                                        (Row)rowsGtTt[k].getAttribute("HtFileView");
                                                                    if (fileViewRow !=
                                                                        null) {
                                                                        String tenFile =
                                                                            (String)fileViewRow.getAttribute("TenFile");
                                                                        if (tenFile !=
                                                                            null) {
                                                                            mapValueKtv.put(idKtvTt.toString(),
                                                                                            tenFile);
                                                                        }
                                                                    }
                                                                }
                                                            }
                                                        }
                                                    }
                                                    listValue.add(mapValueKtv);
                                                }
                                            }
                                        }
                                    tableKtv.setValue(listValue);
                                    RichPanelBorderLayout borderTable=new RichPanelBorderLayout();
                                    borderTable.setStyleClass("subtable");
                                borderTable.getChildren().add(tableKtv);
                                RichPanelLabelAndMessage label =
                                    new RichPanelLabelAndMessage();
                                label.setLabel(ten);
                                label.getChildren().add(borderTable);
                                chiTietPanel.getChildren().add(label);
                                    }
                                }
                            }
                            if (kieu ==
                                Utils.ENUM_KIEU_DU_LIEU.IN_OUT_KIEM_TOAN_VIEN.getId()) {
                                RichTable tableKtv = new RichTable();
                                tableKtv.setAutoHeightRows(1000);
                                tableKtv.setStyleClass("ubck-table AFStretchWidth");
                                tableKtv.setRowBandingInterval(1);
                                tableKtv.setColumnResizing("disabled");
                                tableKtv.setContentDelivery("immediate");
                                tableKtv.setInlineStyle("height:auto !important;");
                                tableKtv.setVar("row");
                                tableKtv.setFetchSize(1000);
                                
                                FacesContext fc =
                                    FacesContext.getCurrentInstance();
                                Application app = fc.getApplication();
                                ExpressionFactory elFactory =
                                    app.getExpressionFactory();
                                ELContext elContext = fc.getELContext();
                                RichColumn column = new RichColumn();
                            column.setNoWrap(false);
                                column.setSeparateRows(true);
                                column.setRowHeader(false);
                                column.setHeaderText(getResource("TANG"));
                                RichOutputText outputText =
                                    new RichOutputText();
                                ValueExpression valueExp =
                                    elFactory.createValueExpression(elContext,
                                                                    "#{row['Tang']}",
                                                                    Object.class);
                                outputText.setValueExpression("value",
                                                              valueExp);
                                column.getChildren().add(outputText);
                                tableKtv.getChildren().add(column);
                                column = new RichColumn();
                            column.setNoWrap(false);
                                column.setHeaderText(getResource("GIAM"));
                                outputText = new RichOutputText();
                                valueExp =
                                        elFactory.createValueExpression(elContext,
                                                                        "#{row['Giam']}",
                                                                        Object.class);
                                outputText.setValueExpression("value",
                                                              valueExp);
                                column.getChildren().add(outputText);
                                tableKtv.getChildren().add(column);
                                column = new RichColumn();
                            column.setNoWrap(false);
                                column.setHeaderText(getResource("THOI_GIAN"));
                                outputText = new RichOutputText();
                                {
            DateTimeConverter converter = (DateTimeConverter)FacesContext.getCurrentInstance().getApplication()
            .createConverter("javax.faces.DateTime");
                                converter.setLocale(new Locale(langCode));
                                converter.setTimeZone(TimeZone.getTimeZone("GMT+7:00"));
                                converter.setType("date");
                                converter.setDateStyle("short");
                                outputText.setConverter(converter);
                                }
                                valueExp =
                                        elFactory.createValueExpression(elContext,
                                                                        "#{row['ThoiGian']}",
                                                                        Object.class);
                                outputText.setValueExpression("value",
                                                              valueExp);
                                column.getChildren().add(outputText);
                                tableKtv.getChildren().add(column);
                                List<String> listSelect = new ArrayList();
                                {
                                    List<AdvancedSearchGroup> listASG =
                                        new ArrayList();
                                    AdvancedSearchGroup asgTrangThai =
                                        new AdvancedSearchGroup();
                                    listASG.add(asgTrangThai);
                                    AdvancedSearchItem asiTrangThai =
                                        new AdvancedSearchItem();
                                    asgTrangThai.getListChildren().add(asiTrangThai);
                                    asiTrangThai.setItemName("TrangThai");
                                    asiTrangThai.setRequired(true);
                                    asiTrangThai.getListValue().add(0,
                                                                    new Number(Utils.ENUM_TRANG_THAI.CONG_BO.getId()));
                                    AdvancedSearchGroup asgLoaiTruongTt =
                                        new AdvancedSearchGroup();
                                    listASG.add(asgLoaiTruongTt);
                                    AdvancedSearchItem asiLoaiTruongTt =
                                        new AdvancedSearchItem();
                                    asgLoaiTruongTt.getListChildren().add(asiLoaiTruongTt);
                                    asiLoaiTruongTt.setItemName("DlctLoaiTruongTt");
                                    asiLoaiTruongTt.setRequired(true);
                                    asiLoaiTruongTt.getListValue().add(0,
                                                                       new Number(Utils.ENUM_LOAI_TRUONG_TT.KIEM_TOAN_VIEN.getId()));
                                    if (langCode != null) {
                                        try {
                                            AdvancedSearchGroup asg =
                                                new AdvancedSearchGroup();
                                            listASG.add(asg);
                                            AdvancedSearchItem asi =
                                                new AdvancedSearchItem();
                                            asg.getListChildren().add(asi);
                                            asi.setItemName("DlctNgonNguView");
                                            asi.setRequired(true);
                                            asi.setOperator(AdvancedSearchItem.OPER_EXISTS);
                                            AdvancedSearchGroup asgChild =
                                                new AdvancedSearchGroup();
                                            asi.getListChildren().add(asgChild);
                                            AdvancedSearchItem asiChild =
                                                new AdvancedSearchItem();
                                            asgChild.getListChildren().add(asiChild);
                                            asiChild.setItemName("Ma");
                                            asiChild.setRequired(true);
                                            asiChild.getListValue().add(langCode);
                                        } catch (Exception e) {
                                        }
                                    }
                                    Utils.searchadvanced("DlctTruongTtView2Iterator",
                                                         listASG);
                                    DCIteratorBinding truongTtView2Iterator =
                                        Utils.getIteratorBinding("DlctTruongTtView2Iterator");
                                    truongTtView2Iterator.setRangeSize(-1);
                                    Row[] rowsKtv =
                                        truongTtView2Iterator.getAllRowsInRange();
        if(rowsKtv==null){
            rowsKtv=new Row[]{};
        }
                                    for (int j = 0; j < rowsKtv.length; j++) {
                                        Number kieuDuLieuKtv =
                                            (Number)rowsKtv[j].getAttribute("KieuDuLieu");
                                        Number viTriHienThiKtv =
                                            (Number)rowsKtv[j].getAttribute("ViTriHienThi");
                                        Row loopRowKtv = rowsKtv[j];
                                        Row rootRowKtv = null;
                                        while (loopRowKtv != null) {
                                            rootRowKtv = loopRowKtv;
                                            loopRowKtv =
                                                    (Row)loopRowKtv.getAttribute("DlctTruongTtTuIdDlctTruongTtView");
                                        }
                                        if (viTriHienThiKtv != null &&
                                            kieuDuLieuKtv != null) {
                                            int viTriKtv =
                                                viTriHienThiKtv.intValue();
                                            int kieuKtv =
                                                kieuDuLieuKtv.intValue();
                                            if ((Utils.ENUM_VI_TRI_HIEN_THI.PORTLET_SELECT.getId() &
                                                 viTriKtv) ==
                                                Utils.ENUM_VI_TRI_HIEN_THI.PORTLET_SELECT.getId()) {
                                                String rootIdKtv =
                                                    ((Number)rootRowKtv.getAttribute("Id")).toString();
                                                if (kieuKtv ==
                                                    Utils.ENUM_KIEU_DU_LIEU.IN_OUT_KIEM_TOAN_VIEN.getId() ||
                                                    kieuKtv ==
                                                    Utils.ENUM_KIEU_DU_LIEU.KIEM_TOAN_VIEN.getId() ||
                                                    kieuKtv ==
                                                    Utils.ENUM_KIEU_DU_LIEU.SL_KIEM_TOAN_VIEN.getId()) {
                                                    // khong dua ra giao dien bang
                                                } else {
                                                    if (listSelect.contains("" +
                                                                            rootIdKtv) ==
                                                        false) {
                                                        listSelect.add("" +
                                                                       rootIdKtv);
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                                {
                                    Calendar calendar=Calendar.getInstance();
                                    Date current =
                                        new Date(new Timestamp(calendar.getTimeInMillis()));
                                    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/mm/dd");
                                    java.util.   Date  javaDate=null;
                                    try {
                                        javaDate =
                                                dateFormat.parse(calendar.get(Calendar.YEAR)+"/01/01");
                                    } catch (ParseException e) {
                                    }
                                    Date dauNam=
                                        new Date(new Timestamp(javaDate.getTime()));
                                    List<AdvancedSearchGroup> listASG =
                                        new ArrayList();
                                    AdvancedSearchGroup asg = new AdvancedSearchGroup();
                                    listASG.add(asg);
                                    AdvancedSearchItem asi = new AdvancedSearchItem();
                                    asg.getListChildren().add(asi);
                                    asi.setItemName("NgayBatDau");
                                    asi.setOperator(asi.OPER_AFTER);
                                    asi.setConjunction(asi.VC_CONJ_OR);
                                    asi.setRequired(true);
                                    asi.getListValue().add(0, dauNam);
                                    asi = new AdvancedSearchItem();
                                    asg.getListChildren().add(asi);
                                    asi.setItemName("NgayKetThuc");
                                    asi.setOperator(asi.OPER_AFTER);
                                    asi.setConjunction(asi.VC_CONJ_OR);
                                    asi.setRequired(true);
                                    asi.getListValue().add(0, dauNam);
                                    Utils.searchadvanced("DlctKtvDvView1Iterator",
                                                         listASG);
                                    DCIteratorBinding ktvDvView1Iterator =
                                        Utils.getIteratorBinding("DlctKtvDvView1Iterator");
                                    ktvDvView1Iterator.setRangeSize(-1);
                                    Row[] rowsKtv =
                                        ktvDvView1Iterator.getAllRowsInRange();
                                    List<Map<String, Object>> listValue =
                                        new ArrayList();
                                    if (rowsKtv != null&&rowsKtv.length>0) {
                                        for (int j = 0; j < rowsKtv.length;
                                             j++) {
                                            ktvDvView1Iterator.setCurrentRowIndexInRange(j);
                                            Row kiemToanVienRow =
                                                (Row)ktvDvView1Iterator.getCurrentRow().getAttribute("DlctKiemToanVienView");
                                            if (kiemToanVienRow != null) {
                                                RowSet rowSetGtTt =
                                                    (RowSet)kiemToanVienRow.getAttribute("DlctGtKtvTtView");
                                                if (rowSetGtTt != null) {
                                    rowSetGtTt.setRangeSize(-1);
                                                    Row[] rowsGtTt =
                                                        rowSetGtTt.getAllRowsInRange();
        if(rowsGtTt==null){
            rowsGtTt=new Row[]{};
        }
                                                    HashMap<String, Object> mapValueKtv =
                                                        new HashMap();
                                                    for (int k = 0;
                                                         k < rowsGtTt.length;
                                                         k++) {
                                                        Row truongTtRow =
                                                            (Row)rowsGtTt[k].getAttribute("DlctTruongTtView");
                                                        if (truongTtRow !=
                                                            null) {
                                                            Number idKtvTt =
                                                                (Number)truongTtRow.getAttribute("Id");
                                                            Number kieuDuLieuKtvTt =
                                                                (Number)truongTtRow.getAttribute("KieuDuLieu");
                                                            if (idKtvTt !=
                                                                null &&
                                                                kieuDuLieuKtvTt !=
                                                                null) {
                                                                int kieuKtvTt =
                                                                    kieuDuLieuKtvTt.intValue();
                                                                if (kieuKtvTt ==
                                                                    Utils.ENUM_KIEU_DU_LIEU.NUMBER.getId()) {
                                                                    Number valNumber =
                                                                        (Number)rowsGtTt[k].getAttribute("ValNumber");
                                                                    if (valNumber !=
                                                                        null) {
                                                                        mapValueKtv.put(idKtvTt.toString(),
                                                                                        valNumber.toString());
                                                                    }
                                                                }
                                                                if (kieuKtvTt ==
                                                                    Utils.ENUM_KIEU_DU_LIEU.VARCHAR.getId()) {
                                                                    String valVarchar =
                                                                        (String)rowsGtTt[k].getAttribute("ValVarchar");
                                                                    if (valVarchar !=
                                                                        null) {
                                                                        mapValueKtv.put(idKtvTt.toString(),
                                                                                        valVarchar);
                                                                    }
                                                                }
                                                                if (kieuKtvTt ==
                                                                    Utils.ENUM_KIEU_DU_LIEU.LINK.getId()) {
                                                                    String valVarchar =
                                                                        (String)rowsGtTt[k].getAttribute("ValVarchar");
                                                                    if (valVarchar !=
                                                                        null) {
                                                                        mapValueKtv.put(idKtvTt.toString(),
                                                                                        valVarchar);
                                                                    }
                                                                }
                                                                if (kieuKtvTt ==
                                                                    Utils.ENUM_KIEU_DU_LIEU.EMAIL.getId()) {
                                                                    String valVarchar =
                                                                        (String)rowsGtTt[k].getAttribute("ValVarchar");
                                                                    if (valVarchar !=
                                                                        null) {
                                                                        mapValueKtv.put(idKtvTt.toString(),
                                                                                        valVarchar);
                                                                    }
                                                                }
                                                                if (kieuKtvTt ==
                                                                    Utils.ENUM_KIEU_DU_LIEU.DATE.getId()) {
                                                                    oracle.jbo.domain.Date valDate =
                                                                        (oracle.jbo.domain.Date)rowsGtTt[k].getAttribute("ValDate");
                                                                    if (valDate !=
                                                                        null) {
                                                                        java.util.Date date =
                                                                            valDate.dateValue();
                                                                        if (date !=
                                                                            null) {
                                                                            mapValueKtv.put(idKtvTt.toString(),
                                                                                            date);
                                                                        }
                                                                    }
                                                                }
                                                                if (kieuKtvTt ==
                                                                    Utils.ENUM_KIEU_DU_LIEU.CLOB.getId()) {
                                                                    ClobDomain valClob =
                                                                        (ClobDomain)rowsGtTt[k].getAttribute("ValClob");
                                                                    if (valClob !=
                                                                        null) {
                                                                        mapValueKtv.put(idKtvTt.toString(),
                                                                                        valClob.toString());
                                                                    }
                                                                }
                                                                if (kieuKtvTt ==
                                                                    Utils.ENUM_KIEU_DU_LIEU.FILE.getId()) {
                                                                    Row fileViewRow =
                                                                        (Row)rowsGtTt[k].getAttribute("HtFileView");
                                                                    if (fileViewRow !=
                                                                        null) {
                                                                        String tenFile =
                                                                            (String)fileViewRow.getAttribute("TenFile");
                                                                        if (tenFile !=
                                                                            null) {
                                                                            mapValueKtv.put(idKtvTt.toString(),
                                                                                            tenFile);
                                                                        }
                                                                    }
                                                                }
                                                            }
                                                        }
                                                    }
                                                    {
                                                        oracle.jbo.domain.Date valDateBatDau =
                                                            (oracle.jbo.domain.Date)ktvDvView1Iterator.getCurrentRow().getAttribute("NgayBatDau");
                                                        oracle.jbo.domain.Date valDateKetThuc =
                                                            (oracle.jbo.domain.Date)ktvDvView1Iterator.getCurrentRow().getAttribute("NgayKetThuc");
                                                        if (valDateKetThuc ==
                                                            null ||
                                                            valDateKetThuc.dateValue().after(current.dateValue())) {
                                                            String selectValue =
                                                                "";
                                                            for (String str :
                                                                 listSelect) {
                                                                Object obj =
                                                                    mapValueKtv.get(str);
                                                                if (obj !=
                                                                    null) {
                                                                    selectValue =
                                                                            selectValue +
                                                                            " " +
                                                                            obj;
                                                                }
                                                            }
                                                            if (valDateBatDau !=
                                                                null &&
                                                                selectValue.isEmpty() ==
                                                                false) {
                                                                mapValueKtv =
                                                                        new HashMap();
                                                                java.util.Date date =
                                                                    valDateBatDau.dateValue();
                                                                mapValueKtv.put("Tang",
                                                                                selectValue);
                                                                if (date !=
                                                                    null) {
                                                                    mapValueKtv.put("ThoiGian",
                                                                                    date);
                                                                }
                                                                listValue.add(mapValueKtv);
                                                            }
                                                        } else {
                                                            String selectValue =
                                                                "";
                                                            for (String str :
                                                                 listSelect) {
                                                                Object obj =
                                                                    mapValueKtv.get(str);
                                                                if (obj !=
                                                                    null) {
                                                                    selectValue =
                                                                            selectValue +
                                                                            " " +
                                                                            obj;
                                                                }
                                                            }
                                                            if (valDateKetThuc !=
                                                                null &&
                                                                selectValue.isEmpty() ==
                                                                false) {
                                                                mapValueKtv =
                                                                        new HashMap();
                                                                java.util.Date date =
                                                                    valDateKetThuc.dateValue();
                                                                mapValueKtv.put("Giam",
                                                                                selectValue);
                                                                if (date !=
                                                                    null) {
                                                                    mapValueKtv.put("ThoiGian",
                                                                                    date);
                                                                }
                                                                listValue.add(mapValueKtv);
                                                            }
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    Collections.sort(listValue, new ThoiGianComparator());
                                    tableKtv.setValue(listValue);
                                    RichPanelBorderLayout borderTable=new RichPanelBorderLayout();
                                    borderTable.setStyleClass("subtable");
                                borderTable.getChildren().add(tableKtv);
                                RichPanelLabelAndMessage label =
                                    new RichPanelLabelAndMessage();
                                label.setLabel(ten);
                                label.getChildren().add(borderTable);
                                chiTietPanel.getChildren().add(label);
                                    }
                                }
                            }
                        } else {
                            Object valObject = mapValue.get(rootId);
                            if (valObject != null) {
                                if (kieu ==
                                    Utils.ENUM_KIEU_DU_LIEU.NUMBER.getId() ||
                                    kieu ==
                                    Utils.ENUM_KIEU_DU_LIEU.VARCHAR.getId() ||
                                    kieu ==
                                    Utils.ENUM_KIEU_DU_LIEU.DATE.getId() ||
                                    kieu ==
                                    Utils.ENUM_KIEU_DU_LIEU.LOAI_CHUNG_KHOAN.getId() ||
                                    kieu ==
                                    Utils.ENUM_KIEU_DU_LIEU.SAN_GIAO_DICH.getId()) {
                                    RichPanelLabelAndMessage label =
                                        new RichPanelLabelAndMessage();
                                    label.setLabel(ten);
                                    RichOutputText outputText =
                                        new RichOutputText();
                                    outputText.setValue(valObject);
                                    if (kieu ==Utils.ENUM_KIEU_DU_LIEU.DATE.getId()){
            DateTimeConverter converter = (DateTimeConverter)FacesContext.getCurrentInstance().getApplication()
            .createConverter("javax.faces.DateTime");
                                converter.setLocale(new Locale(langCode));
                                converter.setTimeZone(TimeZone.getTimeZone("GMT+7:00"));
                                converter.setType("date");
                                converter.setDateStyle("short");
                                    outputText.setConverter(converter);
                                    }
                                    label.getChildren().add(outputText);
                                    chiTietPanel.getChildren().add(label);
                                }
                                if (kieu ==
                                    Utils.ENUM_KIEU_DU_LIEU.CLOB.getId()) {
                                    RichPanelLabelAndMessage label =
                                        new RichPanelLabelAndMessage();
                                    label.setLabel(ten);
                                    RichOutputText outputFormatted =
                                        new RichOutputText();
                                    outputFormatted.setEscape(false);
                                    outputFormatted.setValue(valObject);
                                    label.getChildren().add(outputFormatted);
                                    chiTietPanel.getChildren().add(label);
                                }
                                if (kieu ==
                                    Utils.ENUM_KIEU_DU_LIEU.LINK.getId()) {
                                    RichPanelLabelAndMessage label =
                                        new RichPanelLabelAndMessage();
                                    label.setLabel(ten);
                                    RichGoLink goLink = new RichGoLink();
                            goLink.setTargetFrame("_parent");
                                    goLink.setInlineStyle("padding:5px 7px;");
                                    goLink.setText((String)valObject);
                                    String link=(String)valObject;
                                    if(link!=null){
                                        link=link.toLowerCase();
                                        if(link.startsWith("http://")==false){
                                            link="http://"+link;
                                        }
                                    goLink.setDestination(link);
                                    }
                                    label.getChildren().add(goLink);
                                    chiTietPanel.getChildren().add(label);
                                }
                                if (kieu ==
                                    Utils.ENUM_KIEU_DU_LIEU.EMAIL.getId()) {
                                    RichPanelLabelAndMessage label =
                                        new RichPanelLabelAndMessage();
                                    label.setLabel(ten);
                                    RichGoLink goLink = new RichGoLink();
                            goLink.setTargetFrame("_parent");
                                    goLink.setInlineStyle("padding:5px 7px;");
                                    goLink.setText((String)valObject);
                                    goLink.setDestination("mailto:" +
                                                          valObject);
                                    label.getChildren().add(goLink);
                                    chiTietPanel.getChildren().add(label);
                                }
                                if (kieu ==
                                    Utils.ENUM_KIEU_DU_LIEU.FILE.getId()) {
                                    String htFileServletUrl=
                                    (String)request.getPortletSession().getAttribute("htfileservleturl");
                                    Row row = (Row)valObject;
                                    RichPanelLabelAndMessage label =
                                        new RichPanelLabelAndMessage();
                                    label.setLabel(ten);
                                    RichGoLink goLink = new RichGoLink();
                            goLink.setTargetFrame("_parent");
                                    goLink.setInlineStyle("padding:5px 7px;");
                                    goLink.setText((String)row.getAttribute("TenFile"));
                                    goLink.setDestination(htFileServletUrl+"?id=" +
                                                          row.getAttribute("Id"));
                                    label.getChildren().add(goLink);
                                    chiTietPanel.getChildren().add(label);
                                }
                                if (kieu ==
                                    Utils.ENUM_KIEU_DU_LIEU.LOGO.getId()) {
                                    String htFileServletUrl=
                                    (String)request.getPortletSession().getAttribute("htfileservleturl");
                                    Row row = (Row)valObject;
                                    RichPanelLabelAndMessage label =
                                        new RichPanelLabelAndMessage();
                                    label.setLabel(ten);
                                    RichImage image=new RichImage();
                                    image.setSource(htFileServletUrl+"?id=" +
                                                          row.getAttribute("Id"));
                                    label.getChildren().add(image);
                                    chiTietPanel.getChildren().add(label);
                                }
                            }
                        }
                    }
                }
            }
        }
    }

    private HashMap<String, Object> getValue(PortletRequest request) {
        HashMap<String, Object> mapValue = new HashMap();
        try {
            String donViRowKey =
                (String)request.getPortletSession().getAttribute("donviid");
            if (donViRowKey != null) {
                DCIteratorBinding donViView1Iterator =
                    Utils.getIteratorBinding("DlctDonViView1Iterator");
                if(donViView1Iterator.findRowByKeyString(donViRowKey)!=null){
                donViView1Iterator.setCurrentRowWithKey(donViRowKey);
                DCIteratorBinding gtDvTtView1Iterator =
                    Utils.getIteratorBinding("DlctGtDvTtView1Iterator");
                                    gtDvTtView1Iterator.setRangeSize(-1);
                Row[] rowsGtTt = gtDvTtView1Iterator.getAllRowsInRange();
                if(rowsGtTt==null){
                    rowsGtTt=new Row[]{};
                }
                for (int j = 0; j < rowsGtTt.length; j++) {
                    Row truongTtRow =
                        (Row)rowsGtTt[j].getAttribute("DlctTruongTtView");
                    if (truongTtRow != null) {
                        Number id = (Number)truongTtRow.getAttribute("Id");
                        Number kieuDuLieu =
                            (Number)truongTtRow.getAttribute("KieuDuLieu");
                        if (id != null && kieuDuLieu != null) {
                            int kieu = kieuDuLieu.intValue();
                            if (kieu ==
                                Utils.ENUM_KIEU_DU_LIEU.NUMBER.getId()) {
                                Number valNumber =
                                    (Number)rowsGtTt[j].getAttribute("ValNumber");
                                if (valNumber != null) {
                                    mapValue.put(id.toString(),
                                                 valNumber.toString());
                                }
                            }
                            if (kieu ==
                                Utils.ENUM_KIEU_DU_LIEU.VARCHAR.getId()) {
                                String valVarchar =
                                    (String)rowsGtTt[j].getAttribute("ValVarchar");
                                if (valVarchar != null) {
                                    mapValue.put(id.toString(), valVarchar);
                                }
                            }
                            if (kieu == Utils.ENUM_KIEU_DU_LIEU.LINK.getId()) {
                                String valVarchar =
                                    (String)rowsGtTt[j].getAttribute("ValVarchar");
                                if (valVarchar != null) {
                                    mapValue.put(id.toString(), valVarchar);
                                }
                            }
                            if (kieu ==
                                Utils.ENUM_KIEU_DU_LIEU.EMAIL.getId()) {
                                String valVarchar =
                                    (String)rowsGtTt[j].getAttribute("ValVarchar");
                                if (valVarchar != null) {
                                    mapValue.put(id.toString(), valVarchar);
                                }
                            }
                            if (kieu == Utils.ENUM_KIEU_DU_LIEU.DATE.getId()) {
                                oracle.jbo.domain.Date valDate =
                                    (oracle.jbo.domain.Date)rowsGtTt[j].getAttribute("ValDate");
                                if (valDate != null) {
                                    java.util.Date date = valDate.dateValue();
                                    if (date != null) {
                                        mapValue.put(id.toString(), date);
                                    }
                                }
                            }
                            if (kieu == Utils.ENUM_KIEU_DU_LIEU.CLOB.getId()) {
                                ClobDomain valClob =
                                    (ClobDomain)rowsGtTt[j].getAttribute("ValClob");
                                if (valClob != null) {
                                    mapValue.put(id.toString(),
                                                 valClob.toString());
                                }
                            }
                            if (kieu == Utils.ENUM_KIEU_DU_LIEU.FILE.getId()) {
                                Row fileViewRow =
                                    (Row)rowsGtTt[j].getAttribute("HtFileView");
                                if (fileViewRow != null) {
                                        mapValue.put(id.toString(), fileViewRow);
                                }
                            }
                            if (kieu == Utils.ENUM_KIEU_DU_LIEU.LOGO.getId()) {
                                Row fileViewRow =
                                    (Row)rowsGtTt[j].getAttribute("HtFileView");
                                if (fileViewRow != null) {
                                        mapValue.put(id.toString(), fileViewRow);
                                }
                            }
                            if (kieu ==
                                Utils.ENUM_KIEU_DU_LIEU.LOAI_CHUNG_KHOAN.getId()) {
                                Row loaiChungKhoanRow =
                                    (Row)rowsGtTt[j].getAttribute("DlctLoaiChungKhoanView");
                                if (loaiChungKhoanRow != null) {
                                    RowSet gtLckTtRowSet =
                                        (RowSet)loaiChungKhoanRow.getAttribute("DlctGtLckTtView");
                                    String ten = "";
                                    if (gtLckTtRowSet != null) {
                                        gtLckTtRowSet.setRangeSize(-1);
                                        Row[] gtLckTtRows =
                                            gtLckTtRowSet.getAllRowsInRange();
                                        if(gtLckTtRows==null){
                                            gtLckTtRows=new Row[]{};
                                        }
                                        for (int k = 0; k < gtLckTtRows.length;
                                             k++) {
                                            Row gtLckTtTruongTtRow =
                                                (Row)gtLckTtRows[k].getAttribute("DlctTruongTtView");
                                            Number gtLckTtKieuDuLieu =
                                                (Number)gtLckTtTruongTtRow.getAttribute("KieuDuLieu");
                                            Number gtLckTtViTriHienThi =
                                                (Number)gtLckTtTruongTtRow.getAttribute("ViTriHienThi");
                                            if (gtLckTtViTriHienThi != null &&
                                                gtLckTtKieuDuLieu != null) {
                                                int gtLckviTri =
                                                    gtLckTtViTriHienThi.intValue();
                                                int gtLckKieu =
                                                    gtLckTtKieuDuLieu.intValue();
                                                if ((Utils.ENUM_VI_TRI_HIEN_THI.PORTLET_SELECT.getId() &
                                                     gtLckviTri) ==
                                                    Utils.ENUM_VI_TRI_HIEN_THI.PORTLET_SELECT.getId()) {
                                                    if (gtLckKieu ==
                                                        Utils.ENUM_KIEU_DU_LIEU.NUMBER.getId()) {
                                                        Number valNumber =
                                                            (Number)gtLckTtRows[k].getAttribute("ValNumber");
                                                        if (valNumber !=
                                                            null) {
                                                            ten =
ten + valNumber.toString() + " ";
                                                        }
                                                    }
                                                    if (gtLckKieu ==
                                                        Utils.ENUM_KIEU_DU_LIEU.VARCHAR.getId()) {
                                                        String valVarchar =
                                                            (String)gtLckTtRows[k].getAttribute("ValVarchar");
                                                        if (valVarchar !=
                                                            null) {
                                                            ten =
ten + valVarchar + " ";
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                    mapValue.put(id.toString(), ten);
                                }
                            }
                            if (kieu ==
                                Utils.ENUM_KIEU_DU_LIEU.SAN_GIAO_DICH.getId()) {
                                Row sanGiaoDichRow =
                                    (Row)rowsGtTt[j].getAttribute("DlctSanGiaoDichView");
                                if (sanGiaoDichRow != null) {
                                    RowSet gtSgdTtRowSet =
                                        (RowSet)sanGiaoDichRow.getAttribute("DlctGtSgdTtView");
                                    String ten = "";
                                    if (gtSgdTtRowSet != null) {
                                    gtSgdTtRowSet.setRangeSize(-1);
                                        Row[] gtSgdTtRows =
                                            gtSgdTtRowSet.getAllRowsInRange();
        if(gtSgdTtRows==null){
            gtSgdTtRows=new Row[]{};
        }
                                        for (int k = 0; k < gtSgdTtRows.length;
                                             k++) {
                                            Row gtLckTtTruongTtRow =
                                                (Row)gtSgdTtRows[k].getAttribute("DlctTruongTtView");
                                            Number gtLckTtKieuDuLieu =
                                                (Number)gtLckTtTruongTtRow.getAttribute("KieuDuLieu");
                                            Number gtLckTtViTriHienThi =
                                                (Number)gtLckTtTruongTtRow.getAttribute("ViTriHienThi");
                                            if (gtLckTtViTriHienThi != null &&
                                                gtLckTtKieuDuLieu != null) {
                                                int gtLckviTri =
                                                    gtLckTtViTriHienThi.intValue();
                                                int gtLckKieu =
                                                    gtLckTtKieuDuLieu.intValue();
                                                if ((Utils.ENUM_VI_TRI_HIEN_THI.PORTLET_SELECT.getId() &
                                                     gtLckviTri) ==
                                                    Utils.ENUM_VI_TRI_HIEN_THI.PORTLET_SELECT.getId()) {
                                                    if (gtLckKieu ==
                                                        Utils.ENUM_KIEU_DU_LIEU.NUMBER.getId()) {
                                                        Number valNumber =
                                                            (Number)gtSgdTtRows[k].getAttribute("ValNumber");
                                                        if (valNumber !=
                                                            null) {
                                                            ten =
ten + valNumber.toString() + " ";
                                                        }
                                                    }
                                                    if (gtLckKieu ==
                                                        Utils.ENUM_KIEU_DU_LIEU.VARCHAR.getId()) {
                                                        String valVarchar =
                                                            (String)gtSgdTtRows[k].getAttribute("ValVarchar");
                                                        if (valVarchar !=
                                                            null) {
                                                            ten =
ten + valVarchar + " ";
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                    mapValue.put(id.toString(), ten);
                                }
                            }
                        }
                    }
                }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            mapValue = null;
        }
        return mapValue;
    }

    private void resetIterator(PortletRequest request) {
        String loaiDonViId =
            (String)request.getPortletSession().getAttribute("loaidonviid");
        String langCode =
            (String)request.getPortletSession().getAttribute("langcode");
        if (langCode == null) {
            langCode = DEFAULT_LANGCODE;
        }

        List<AdvancedSearchGroup> listASG = new ArrayList();
        AdvancedSearchGroup asgTrangThai = new AdvancedSearchGroup();
        listASG.add(asgTrangThai);
        AdvancedSearchItem asiTrangThai = new AdvancedSearchItem();
        asgTrangThai.getListChildren().add(asiTrangThai);
        asiTrangThai.setItemName("TrangThai");
        asiTrangThai.setRequired(true);
        asiTrangThai.getListValue().add(0,
                                        new Number(Utils.ENUM_TRANG_THAI.CONG_BO.getId()));
        if (loaiDonViId != null) {
            try {
                Number loaiDonViNumber = new Number(loaiDonViId);
                AdvancedSearchGroup asg = new AdvancedSearchGroup();
                listASG.add(asg);
                AdvancedSearchItem asi = new AdvancedSearchItem();
                asg.getListChildren().add(asi);
                asi.setItemName("DlctDvLdvView");
                asi.setRequired(true);
                asi.setOperator(AdvancedSearchItem.OPER_EXISTS);
                AdvancedSearchGroup asgChild = new AdvancedSearchGroup();
                asi.getListChildren().add(asgChild);
                AdvancedSearchItem asiChild = new AdvancedSearchItem();
                asgChild.getListChildren().add(asiChild);
                asiChild.setItemName("DlctLoaiDonViId");
                asiChild.setRequired(true);
                asiChild.getListValue().add(loaiDonViNumber);
            } catch (Exception e) {
            }
        }
        if (langCode != null) {
            try {
                AdvancedSearchGroup asg = new AdvancedSearchGroup();
                listASG.add(asg);
                AdvancedSearchItem asi = new AdvancedSearchItem();
                asg.getListChildren().add(asi);
                asi.setItemName("DlctDvNnView");
                asi.setRequired(true);
                asi.setOperator(AdvancedSearchItem.OPER_EXISTS);
                AdvancedSearchGroup asgChild = new AdvancedSearchGroup();
                asi.getListChildren().add(asgChild);
                AdvancedSearchItem asiChild = new AdvancedSearchItem();
                asgChild.getListChildren().add(asiChild);
                asiChild.setItemName("DlctNgonNguView");
                asiChild.setRequired(true);
                asiChild.setOperator(AdvancedSearchItem.OPER_EXISTS);
                AdvancedSearchGroup asgChild2 = new AdvancedSearchGroup();
                asiChild.getListChildren().add(asgChild2);
                AdvancedSearchItem asiChild2 = new AdvancedSearchItem();
                asgChild2.getListChildren().add(asiChild2);
                asiChild2.setItemName("Ma");
                asiChild2.setRequired(true);
                asiChild2.getListValue().add(langCode);
            } catch (Exception e) {
            }
        }
        Utils.searchadvanced("DlctDonViView1Iterator", listASG);

        listASG = new ArrayList();
        asgTrangThai = new AdvancedSearchGroup();
        listASG.add(asgTrangThai);
        asiTrangThai = new AdvancedSearchItem();
        asgTrangThai.getListChildren().add(asiTrangThai);
        asiTrangThai.setItemName("TrangThai");
        asiTrangThai.setRequired(true);
        asiTrangThai.getListValue().add(0,
                                        new Number(Utils.ENUM_TRANG_THAI.CONG_BO.getId()));
        AdvancedSearchGroup asgLoaiTruongTt = new AdvancedSearchGroup();
        listASG.add(asgLoaiTruongTt);
        AdvancedSearchItem asiLoaiTruongTt = new AdvancedSearchItem();
        asgLoaiTruongTt.getListChildren().add(asiLoaiTruongTt);
        asiLoaiTruongTt.setItemName("DlctLoaiTruongTt");
        asiLoaiTruongTt.setRequired(true);
        asiLoaiTruongTt.getListValue().add(0,
                                           new Number(Utils.ENUM_LOAI_TRUONG_TT.DON_VI.getId()));
        if (loaiDonViId != null) {
            try {
                Number loaiDonViNumber = new Number(loaiDonViId);
                AdvancedSearchGroup asg = new AdvancedSearchGroup();
                listASG.add(asg);
                AdvancedSearchItem asi = new AdvancedSearchItem();
                asg.getListChildren().add(asi);
                asi.setItemName("DlctLdvTtView");
                asi.setRequired(true);
                asi.setOperator(AdvancedSearchItem.OPER_EXISTS);
                AdvancedSearchGroup asgChild = new AdvancedSearchGroup();
                asi.getListChildren().add(asgChild);
                AdvancedSearchItem asiChild = new AdvancedSearchItem();
                asgChild.getListChildren().add(asiChild);
                asiChild.setItemName("DlctLoaiDonViId");
                asiChild.setRequired(true);
                asiChild.getListValue().add(loaiDonViNumber);
            } catch (Exception e) {
            }
        }
        if (langCode != null) {
            try {
                AdvancedSearchGroup asg = new AdvancedSearchGroup();
                listASG.add(asg);
                AdvancedSearchItem asi = new AdvancedSearchItem();
                asg.getListChildren().add(asi);
                asi.setItemName("DlctNgonNguView");
                asi.setRequired(true);
                asi.setOperator(AdvancedSearchItem.OPER_EXISTS);
                AdvancedSearchGroup asgChild = new AdvancedSearchGroup();
                asi.getListChildren().add(asgChild);
                AdvancedSearchItem asiChild = new AdvancedSearchItem();
                asgChild.getListChildren().add(asiChild);
                asiChild.setItemName("Ma");
                asiChild.setRequired(true);
                asiChild.getListValue().add(langCode);
            } catch (Exception e) {
            }
        }
        Utils.searchadvanced("DlctTruongTtView1Iterator", listASG);
    }

    private String getResource(String resourceKey) {
        PortletRequest request =
            (PortletRequest)FacesContext.getCurrentInstance().getExternalContext().getRequest();
        String langCode =
            (String)request.getPortletSession().getAttribute("langcode");
        if (langCode == null) {
            langCode = DEFAULT_LANGCODE;
        }
        Locale locale = new Locale(langCode);
        ResourceBundle bundle =
            ResourceBundle.getBundle("portlet.ctct.resource.ChitietCtBundle",
                                     locale);
        return bundle.getString(resourceKey);
    }
    
    public String getTieuDe(){
        PortletRequest request =
            (PortletRequest)FacesContext.getCurrentInstance().getExternalContext().getRequest();
        return request.getParameter("tieude");
    }

    public void setChiTietPanel(RichPanelFormLayout chiTietPanel) {
        this.chiTietPanel = chiTietPanel;
    }

    public RichPanelFormLayout getChiTietPanel() {
        return chiTietPanel;
    }
    
    public static class ThoiGianComparator implements Comparator<Map<String,Object>>{

        public int compare(Map<String, Object> o1, Map<String, Object> o2) {
            if(o1!=null&&o2!=null){
            Object thoiGian1=o1.get("ThoiGian");
            Object thoiGian2=o1.get("ThoiGian");
            if(thoiGian1 instanceof java.util.Date && thoiGian2 instanceof java.util.Date)
                if(thoiGian1!=null&&thoiGian2!=null){
                    return ((java.util.Date)thoiGian1).compareTo((java.util.Date)thoiGian2);
                }else if(thoiGian1!=null){
                return -1;
            }else if(thoiGian2!=null){
                return 1;
                }
            }else if(o1!=null){
                return -1;
            }else if(o2!=null){
                return 1;
                }
            return 0;
        }
    }
}
